package com.banciyuan.bcywebview.biz.topic;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.Special;

/**
 * Created by squall on 2015/8/14.
 */
public class TopicDetailListAdapter extends BaseAdapter {

    private List<Special> slist = new ArrayList<Special>();
    private Context mContext;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;

    public TopicDetailListAdapter(List<Special> slist, Context mContext) {
        this.slist = slist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        imageLoader = ImageLoader.getInstance();
    }

    @Override
    public int getCount() {
        return slist.size();
    }

    @Override
    public Object getItem(int i) {
        return slist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    private class ViewHolder {
        TextView title;
        ImageView pic;

        public ViewHolder(View convertView) {
            pic = (ImageView) convertView.findViewById(R.id.topic_footer_img);
            title = (TextView) convertView.findViewById(R.id.topic_footer_title);
        }
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.topic_list_title_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        Special s = slist.get(i);
        imageLoader.displayImage(s.getCover_link(), viewHolder.pic, ImageOptions.options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                if (null == loadedImage)
                    return;
                int width = CommonUtils.getWidthPixels((Activity) mContext);
                int height = width * loadedImage.getHeight() / loadedImage.getWidth();
                FrameLayout.LayoutParams bannerParams = new FrameLayout.LayoutParams(width, height);
                view.setLayoutParams(bannerParams);
                ((ImageView) view).setImageBitmap(loadedImage);
            }
        }, null, true);
        viewHolder.title.setText(Html.fromHtml(s.getTitle()));
        return view;
    }
}
